<?php
/**
 * Template Name: My Venue
 */

get_header(); ?>

<div class="container" xmlns="http://www.w3.org/1999/html">
    <h1><?php the_title() ?></h1>

    <div class="row-fluid">
        <div class="span5">
            <div class="card grey">
                <a href="#" class="btn btn-primary pull-right">Reports & Statistics &raquo;</a>
                <h2 class="title">Overview</h2>

                <?
                $name = get_user_relation_data('venue', 'name', get_user_id());
                query_posts(array('post_type' => 'pitches', 'showposts' => 100));
                $pitchcount = 0;
                while (have_posts()) : the_post();
                    if (get_venue_name() == $name) {
                        $pitchcount++;
                    }
                endwhile;
                ?>
                <h3><? echo $pitchcount; ?> Pitch(es)</h3>

                <?
                $name = get_user_relation_data('venue', 'name', get_user_id());
                query_posts(array('post_type' => 'pitches', 'showposts' => 100));
                $slotcount = 0;
                while (have_posts()) : the_post();
                    if (get_relation_data('venue', 'name') == $name) {
                        $pid = get_field('pid');
                        $the_query2 = new WP_Query(array('post_type' => 'slots', 'showposts' => 100));
                        while ($the_query2->have_posts()) : $the_query2->the_post();
                            if (get_any_relation_data('pitch', 'pid', $the_query2->post->ID) == $pid) {
                                $slotcount++;
                            }
                        endwhile;
                    }
                endwhile;
                ?>
                <h3><? echo $slotcount; ?> Available Weekly Booking Slot(s)</h3>

                <?
                $name = get_user_relation_data('venue', 'name', get_user_id());
                query_posts(array('post_type' => 'pitches', 'showposts' => 100));
                $bookingCount = 0;
                while (have_posts()) : the_post();
                    if (get_relation_data('venue', 'name') == $name) {
                        $pid = get_field('pid');
                        $the_query2 = new WP_Query(array('post_type' => 'slots', 'showposts' => 100));
                        while ($the_query2->have_posts()) : $the_query2->the_post();
                            if (get_any_relation_data('pitch', 'pid', $the_query2->post->ID) == $pid) {
                                $the_query3 = new WP_Query(array('post_type' => 'bookings', 'showposts' => 100));
                                while ($the_query3->have_posts()) : $the_query3->the_post();
                                    if (get_any_relation_id('slot', $the_query3->post->ID) == $the_query2->post->ID) {
                                        $bookingCount++;
                                    }
                                endwhile;

                            }
                        endwhile;
                    }
                endwhile;
                ?>
                <h3><? echo $bookingCount; ?> Booking(s)</h3>


            </div>
        </div>
        <div class="span7">
            <div class="card grey">
                <div style="height:170px;overflow-y: scroll;">
                    <a href="/venue-bookings/" class="btn btn-primary pull-right">Detailed Bookings &raquo;</a>

                    <h2 class="title"><a href="/venue-bookings/">Bookings</a></h2>
                <table class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>Pitch</th>
                        <th>Start</th>
                        <th>End</th>
                        <th>Date</th>
                        <th>Contact</th>
                    </tr>
                    </thead>
                <tbody>
                    <?
                    $name = get_user_relation_data('venue', 'name', get_user_id());
                    query_posts(array('post_type' => 'pitches', 'showposts' => 100));
                    $bookingCount = 0;
                    while (have_posts()) : the_post();
                        if (get_relation_data('venue', 'name') == $name) {
                            $pid = get_field('pid');
                            $the_query2 = new WP_Query(array('post_type' => 'slots', 'showposts' => 100));
                            while ($the_query2->have_posts()) : $the_query2->the_post();
                                if (get_any_relation_data('pitch', 'pid', $the_query2->post->ID) == $pid) {
                                    $the_query3 = new WP_Query(array('post_type' => 'bookings', 'showposts' => 100));
                                    while ($the_query3->have_posts()) : $the_query3->the_post();
                                        if (get_any_relation_id('slot', $the_query3->post->ID) == $the_query2->post->ID) {
                                            $bookingCount++;
                                            ?>

                                                <tr>
                                                    <td><? the_field('pid'); ?></td>
                                                    <td><? the_field('start_time', $the_query2->post->ID); ?></td>
                                                    <td><? the_field('end_time', $the_query2->post->ID); ?></td>
                                                    <td><?

                                                        $date = DateTime::createFromFormat('Ymd', get_field('date', $the_query3->post->ID));
                                                        echo $date->format('l, d M Y');
                                                        ?></td>
                                                    <td>
                                                        <?
                                                        $customer = get_field('customer_id',$the_query3->post->ID);
                                                        $name = get_field('first_name', 'user_'.$customer);
                                                        //echo $customer;
                                                        echo $name;
                                                        ?>
                                                    </td>

                                                </tr>
                                            <?
                                        }
                                    endwhile;
                                }
                            endwhile;
                        }
                    endwhile;
                    ?>
                </tbody>
                </table>
                </div>
            </div>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span4">
            <div class="card grey">
                <a href="/venue-details/" class="btn btn-primary pull-right">Edit Details &raquo;</a>

                <h2 class="title"><a href="/venue-details/">Details</a></h2>

                <form id="venue-details" style="margin:0;">

                    <?php
                    $posts = get_field('venue', 'user_' . get_user_id());
                    if ($posts):
                        foreach ($posts as $post):
                            setup_postdata($post); ?>

                            <h3>Name</h3>
                            <p><?php the_field('name'); ?></p>
                            <h3>Address</h3>
                            <p><?php the_field('address'); ?></p>
                            <h3>Website</h3>
                            <p><?php the_field('website'); ?></p>
                            <h3>Primary Contact Number</h3>
                            <p><?php the_field('primary_contact_number'); ?></p>
                            <h3>Primary Contact Email</h3>
                            <p><?php the_field('primary_contact_email'); ?></p>
                            <h3>Primary Contact Name</h3>
                            <p><?php the_field('primary_contact_name'); ?></p>
                            <h3>Other Contact Details</h3>
                            <p><?php the_field('other_contact_details'); ?></p>


                            <a href="/venue-details/" class="btn btn-primary">Edit Details &raquo;</a>


                            <?php
                        endforeach;
                        wp_reset_postdata();
                    endif;
                    ?>
                </form>
            </div>
        </div>
        <div class="span4">
            <div class="card grey">
                <a href="/venue-pitches/" class="btn btn-primary pull-right">Edit Pitches &raquo;</a>

                <h2 class="title"><a href="/venue-pitches/">Pitches</a></h2>

                <?php
                $name = get_user_relation_data('venue', 'name', get_user_id());
                query_posts(array('post_type' => 'pitches', 'showposts' => 100));
                while (have_posts()) : the_post();
                    if (get_relation_data('venue', 'name') == $name) {
                        ?>

                        <div class="card clearfix" style="border-radius: 3px">
                            <h3><a href="<?php the_permalink(); ?>"><?php the_field('pid'); ?></a></h3>
                            <a href="<?php the_permalink() ?>">
                                <?php $image = wp_get_attachment_image_src(get_field('image_1'), 'pitch-main');
                                if (get_field('image_1')) {
                                    ?>
                                    <img style="margin:0 15px 15px 0; width:300px; float:left; border-radius:3px;" src="<?php echo $image[0]; ?>"
                                         alt="<?php echo get_the_title(get_field('image_1')) ?>"/>
                                    <?php } else { ?>
                                    <img style="margin:0 15px 15px 0; width:300px; float:left; border-radius:3px;"
                                         src="http://pitchwise.co.uk/wp-content/uploads/2012/11/PitchWise-Logo-Green-658x300.png"
                                         alt="<?php echo get_field('pid') ?>"/>
                                    <?php }?>
                            </a>
                            <a href="#" class="btn btn-primary">Edit &raquo;</a>
                            <a href="#" class="btn pull-right">Remove &raquo;</a>
                        </div>

                        <?php
                    }
                endwhile;
                ?>
            </div>
        </div>
        <div class="span4">
            <div class="card grey">
                <a href="/venue-slots/" class="btn btn-primary pull-right">Edit Slots &raquo;</a>

                <h2 class="title">Slots</h2>

                <?php
                $name = get_user_relation_data('venue', 'name', get_user_id());
                query_posts(array('post_type' => 'pitches', 'showposts' => 100));
                while (have_posts()) : the_post();
                    if (get_relation_data('venue', 'name') == $name) {
                        $pid = get_field('pid');
                        ?>
                        <div class="card">
                            <h3><? the_field('pid'); ?></h3>
                            <?
                            $the_query2 = new WP_Query(array('post_type' => 'slots', 'showposts' => 100));
                            $slotcount = 0;
                            while ($the_query2->have_posts()) : $the_query2->the_post();
                                if (get_any_relation_data('pitch', 'pid', $the_query2->post->ID) == $pid) {
                                    $slotcount++;
                                    if ($slotcount == 1) {
                                        ?>
                                <table class="table table-bordered table-striped">
                                    <thead>
                                    <tr>
                                        <th>Start</th>
                                        <th>End</th>
                                        <th>Day</th>
                                        <th>Price</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                   <? } ?>
                                <tr>
                                    <td><? the_field('start_time', $the_query2->post->ID); ?></td>
                                    <td><? the_field('end_time', $the_query2->post->ID); ?></td>
                                    <td><? the_field('day', $the_query2->post->ID); ?></td>
                                    <td>&pound;<? the_field('price', $the_query2->post->ID); ?></td>
                                </tr>
                                    <?
                                }
                            endwhile;
                            if ($slotcount == 0) {
                                echo 'No Slots';
                            } else {
                                ?> </tbody></table><? } ?>
                        </div>
                        <?
                    }
                endwhile;
                ?>

            </div>
        </div>
    </div>
</div>


<?php get_footer(); ?>